package gui;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;


public class Tree extends JPanel {

	private static final long serialVersionUID = -4544079849805005593L;
	
	private JTree tree;
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Korzen");
    protected DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    
    public Tree() {
    	super(new BorderLayout());
	    tree = new JTree(treeModel);
	    tree.setRootVisible(false);
	    JScrollPane treeView = new JScrollPane(tree);
	    add(treeView);
    }
    
    
	public void clear() {
		rootNode.removeAllChildren();
	}
	
	
	public void reload() {
		treeModel.reload();
		
		if (Main.getInstance().getMainFrame().getExpandTreeState())
			for(int i=0; i<tree.getRowCount(); ++i)
				tree.expandRow(i);
	}
	
	
	/**
	 * Adds new child to the tree.
	 * @param parent - parent of the child
	 * @param s - name of the child
	 * @return reference to new tree node.
	 */
	public DefaultMutableTreeNode addChild(DefaultMutableTreeNode parent, String s) {
		DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(s);
		parent.add(newChild);
		return newChild;
	}

	
	/**
	 * Adds node for the highest position.
	 * @param s - name for the node
	 * @return Reference to new created root node.
	 */
	public DefaultMutableTreeNode addRootNode(String s) {
		DefaultMutableTreeNode newRootNode = new DefaultMutableTreeNode(s);
		rootNode.add(newRootNode);
		return newRootNode;
	}

}
